import {useEffect} from 'react';
import {
  sm2ComparePublicKeyHex,
  sm2CompressPublicKeyHex,
  sm2Decrypt,
  sm2Encrypt,
  sm2GenerateKeyPairHex,
  sm2Signature,
  sm2VerifyPublicKey,
  sm2VerifySignature,
  sm3Hash,
  sm4Decrypt,
  sm4Encrypt,
  sm4GenerateKey
} from '@/utils/smCryptoUtils';

export default () => {

  useEffect(() => {
    // const text = '_测_fasde-2023@试_';
    // console.log('原始数据:', text);
    //
    // console.log('sm3Hash:', sm3Hash(text));
    // console.log('------------------------------------------------');
    //
    // const key = sm4GenerateKey();
    // console.log('key:', key);
    // const iv = sm4GenerateKey();
    // console.log('iv:', iv);
    //
    // // let encryptData = sm4Encrypt(text, key); // 加密，默认输出 16 进制字符串，默认使用 pkcs#7 填充（传 pkcs#5 也会走 pkcs#7 填充）
    // let encryptData = sm4Encrypt(text, key, iv); // 加密，cbc 模式
    // console.log('encryptData_原始Hex:', encryptData);
    //
    // let decryptData = sm4Decrypt(encryptData, key, iv); // 解密，cbc 模式
    // console.log('sm4Decrypt:', decryptData);
    // console.log('------------------------------------------------');
    //
    // let keypair = sm2GenerateKeyPairHex();
    //
    // const publicKey = keypair.publicKey; // 公钥
    // const privateKey = keypair.privateKey; // 私钥
    // console.log('publicKey:', publicKey);
    // console.log('privateKey:', privateKey);
    //
    // // 默认生成公钥 130 位太长，可以压缩公钥到 66 位
    // const compressedPublicKey = sm2CompressPublicKeyHex(publicKey); // compressedPublicKey 和 publicKey 等价
    // console.log('compressedPublicKey:', compressedPublicKey);
    // const isEqu = sm2ComparePublicKeyHex(publicKey, compressedPublicKey); // 判断公钥是否等价
    // console.log('comparePublicKeyHex:', isEqu);
    //
    // console.log('verifyPublicKey1:', sm2VerifyPublicKey(publicKey));
    // console.log('verifyPublicKey2:', sm2VerifyPublicKey(compressedPublicKey));
    // console.log('------------------------------------------------');
    //
    // encryptData = sm2Encrypt(text, publicKey); // 加密结果
    // console.log('encryptData:', encryptData);
    // let compressed_encryptData = sm2Encrypt(text, compressedPublicKey); // 加密结果
    // console.log('compressed_encryptData:', compressed_encryptData);
    // decryptData = sm2Decrypt(encryptData, privateKey); // 解密结果
    // console.log('decryptData:', decryptData);
    //
    // // const result = '';
    // // console.log('decryptData:', sm2Decrypt(result, 'e4c6f10cb1a242e38c4b4eaff69cecba557882966e0d4d4ef4541175531695c1'));
    // console.log('------------------------------------------------');
    //
    // // 纯签名 + 生成椭圆曲线点
    // let sigValueHex = sm2Signature(text, privateKey); // 签名
    // console.log('sigValueHex:', sigValueHex);
    // let verifyResult = sm2VerifySignature(text, sigValueHex, publicKey); // 验签结果
    // console.log('verifyResult:', verifyResult);

    // 原始数据和密钥
    const text = '_测_fasde-2023@试_';
    const key = 'c2b34265b8f7c7908685eecda77e825f';
    const iv = 'd534564cdd62927fc78205c6b12b4291';
    const publicKey = "047fea744a02dd5146800f8170a5703066cfe5cd746da3e0f1f12941040299d512023362574282d592aa470d93ecedab6152c27ef52d0b44824084321e413f5fbd";
    const privateKey = "15e95cef57521b8181f3e6e1f238dcb9378dec65c5467a95198e39724e6f66bc";

    console.log('原始数据: ' + text);
    console.log('key: ' + key);
    console.log('iv: ' + iv);
    console.log('publicKey: ' + publicKey);
    console.log('privateKey: ' + privateKey);

    console.log('sm3Hash: ' + sm3Hash(text));
    console.log('------------------------------------------------');

    const encryptData = sm4Encrypt(text, key, iv);
    console.log('sm4Encrypt: ' + encryptData);
    console.log('sm4Decrypt: ' + sm4Decrypt(encryptData, key, iv));
    console.log('------------------------------------------------');

    // SM2 加密解密
    const sm2EncryptData = sm2Encrypt(text, publicKey);
    console.log('encryptData: ' + sm2EncryptData);
    console.log('decryptData: ' + sm2Decrypt(sm2EncryptData, privateKey));
    console.log('------------------------------------------------');

    // SM2 签名验签
    const sigValueHex = sm2Signature(text, privateKey);
    console.log('sigValueHex: ' + sigValueHex);
    console.log('verifyResult: ' + sm2VerifySignature(text, sigValueHex, publicKey));


    console.log("------------------------------------------------");
    const sigValueHex1="304402207c1ee06c37ace48140e096652380ff2b0428d928a9b62977f91883f301cf5e39022064b39918b592f5a05b0abb055008595f4d7920b12475bbc27807cdb9b4a8e893";
    console.log("手动验签verifyResult: " , sm2VerifySignature(text, sigValueHex1, publicKey));

    console.log("------------------------------------------------");
    const encryptData1 ="bb5e5b6e19060d6b5824bca5efe25ecee168bd79f61f793992cec69facd97346d5548340c03d49bd85460b70212b72bd23a4c929bef1389c3f749cba87873d6321b9201525222ef8b716f386e727566f449259e91f260c864fdcf9afb665c2a245d609fe4590eed7a89f802796d96fd2e5ff4996";
    console.log("手动decryptData: " , sm2Decrypt(encryptData1, privateKey));

  }, []);

  return (
    <div>
      <p>This is umi docs.</p>
    </div>
  );
};